#ifndef bsp_test_commom
#define bsp_test_commom 


#include <string.h>
#include <stdlib.h>

typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned int u32;
typedef int s32;
#define UINTPTR u32

typedef void (*XInterruptHandler) (void *InstancePtr);

/**
 * This data type defines an exception handler for a processor.
 * The argument points to the instance of the component
 */
typedef void (*XExceptionHandler) (void *InstancePtr);
typedef void (*Xil_ExceptionHandler)(void *Data);


#ifndef TRUE
#  define TRUE		1U
#endif

#ifndef FALSE
#  define FALSE		0U
#endif

#ifndef NULL
#define NULL		((void*)0)
#endif

#define XIL_COMPONENT_IS_READY     0x11111111U  



/************************************************/
#define XRt1553B_SET_BIT(x, bit) (x |= (1 << bit))    /* 置位第bit位 */
#define XRt1553B_CLEAR_BIT(x, bit) (x &= ~(1 << bit)) /* 清零第bit位 */

/* RT1553B register offsets */
#define XRT53_IMR_OFFSET 0x00      // Interrupt mask register
#define XRT53_CFG1_OFFSET 0x01     // Configuration register 1
#define XRT53_CFG2_OFFSET 0x02     // Configuration register 2
#define XRT53_SRR_OFFSET 0x03      // Start and Reset register
#define XRT53_CSPR_OFFSET 0x03     // Command Stack Pointer Register
#define XRT53_SCWR_OFFSET 0x04     // RT SubAddr Control Word Register
#define XRT53_TTR_OFFSET 0x05      //   Time Tag Register
#define XRT53_ISR_OFFSET 0x06      // Interrupt Status Register
#define XRT53_CFG3_OFFSET 0x07     // Configuration register 3
#define XRT53_CFG4_OFFSET 0x08     //   Configuration register 4
#define XRT53_CFG5_OFFSET 0x09     // Configuration register 5
#define XRT53_CFG6_OFFSET 0x18     // Configuration register 6  BU64348
#define XRT53_LAST_CMD_OFFSET 0x0D // Last Command Register
#define XRT53_SWR_OFFSET 0x0E      // RT Status Word Register
#define XRT53_BIT_OFFSET 0x0F      // RT BIT Word Register

/* CFG1 Register bit positions */
#define XRT53_CFG1_MSG_BUSY 0x0001

/* IMR Register bit positions */
#define XRT53_IMR_EOM 0x01
#define XRT53_IMR_RT_MODECODE 0x02
#define XRT53_IMR_FORMAT_ERR 0x04
// #define XRT53_IMR_ 0x08
#define XRT53_IMR_SA_CTRL_WORD_EOM 0x10
#define XRT53_IMR_CIRC_BUFF_ROLLOVER 0x20
#define XRT53_IMR_TT_ROLLOVER 0x40
#define XRT53_IMR_ADDR_PARITY_ERR 0x80
#define XRT53_IMR_ALL_DISABLE 0x0000
// #define XRT53_IMR_ 0x100
#define XRT53_IMR_HANDSHAKE_FAIL 0x200

/* ISR Register bit positions */
#define XRT53_ISR_MASTER 0x8000
#define XRT53_ISR_CIRC_ROLLOVER 0x0020
#define XRT53_ISR_SACW_EOM 0x0010
#define XRT53_ISR_MODECODE 0x0002
#define XRT53_ISR_EOM 0x0001
#define XRT53_ISR_ERR_MASK 0x6284 // ISR错误掩码 与操作大于0则有错

/* CFG2 Register bit positions */
#define XRT53_CFG2_ENHANCED_INTR_ENABLE 0x8000
#define XRT53_CFG2_OVERWRITE_INVA_DATA 0x0800
#define XRT53_CFG2_CLEAR_TIME_TAG 0x0040
#define XRT53_CFG2_LOAD_TIME_TAG 0x0020
#define XRT53_CFG2_ISR_AUTO_CLEAR 0x0010
#define XRT53_CFG2_INTR_LOW_LEVEL 0x0008
#define XRT53_CFG2_SERVICE_REQ_AUTO_CLR 0x0004
#define XRT53_CFG2_ENHANCED_RT_MEMORY_MANAGEMENT 0x0002
#define XRT53_CFG2_SEPARATE_BROADCAST_DATA 0x0001

/* SSR Register bit positions */
#define XRT53_SSR_RESET 0x0001
#define XRT53_SSR_INTR_RST 0x04

/* CFG3 Register bit positions */
#define XRT53_CFG3_ENHANCED_MODE_ENABLE 0x8000
#define XRT53_CFG3_ILLEGALIZATION_DISABLE 0x0000
#define XRT53_CFG3_ILLEGAL_RX_DISABLE 0x0010
#define XRT53_CFG3_BUSY_RX_DISABLE 0x0008
#define XRT53_CFG3_ENHANCED_MODE_CODE 0x0001

/* CFG4 Register bit positions */
#define XRT53_CFG4_LATCH_RTADDR 0x0008

/* CFG5 Register bit positions */
#define XRT53_CFG5_LATCH_RTADDR 0x0040

/* CFG6 Register bit positions */
#define XRT53_CFG6_ADV_CPU_ACCESS 0x4000
#define XRT53_CFG6_RTADDR_SET 0x0020

/* RT1553B memory offsets */
#define XRT53_STACKA_PTR_OFFSET 0x0100 // Stack Pointer A
#define XRT53_STACKB_PTR_OFFSET 0x0104 // Stack Pointer B

/* cmd stack mask*/
#define XRT53_BLOCK_STATWORDS_BUSAB_LOC 13
#define XRT53_BLOCK_STATWORDS_ROLLOVER_LOC 7
#define XRT53_CMD_WORD_RTADDR_LOC 11
#define XRT53_CMD_WORD_T_OR_R_LOC 10
#define XRT53_CMD_WORD_SADDR_OR_MODE_LOC 5
#define XRT53_BLOCK_STATWORDS_ERR_MASK 0x177F
#define XRT53_BLOCK_STATWORDS_SOM_MASK 0x4000 // start of message

/*mode code selective interrupt table offset */
#define XRT53_MODECODE_INTR_RX_LSB_OFFSET 0x0108
#define XRT53_MODECODE_INTR_RX_MSB_OFFSET 0x0109
#define XRT53_MODECODE_INTR_TX_LSB_OFFSET 0x010A
#define XRT53_MODECODE_INTR_TX_MSB_OFFSET 0x010B
#define XRT53_MODECODE_INTR_BCSTRX_LSB_OFFSET 0x010C
#define XRT53_MODECODE_INTR_BCSTRX_MSB_OFFSET 0x010D
#define XRT53_MODECODE_INTR_BCSTTX_LSB_OFFSET 0x010E
#define XRT53_MODECODE_INTR_BCSTTX_MSB_OFFSET 0x010F

/*Mode Code data offset*/
#define XRT53_MODE_DATA_SYNC_WITH_DATA_OFFSET 0x0111      // SYNCHRONIZE WITH DATA
#define XRT53_MODE_DATA_TX_VEC_WORD_OFFSET 0x0120         // TRANSMIT VECTOR WORD
#define XRT53_MODE_DATA_TX_BIT_WORD_OFFSET 0x0123         // TRANSMIT BIT WORD
#define XRT53_MODE_DATA_SYNC_WITH_BCST_DATA_OFFSET 0x0131 // BROADCAST SYNCHRONIZE WITH DATA

/**********mem organization***********/
#define XRT53_STACK_PTR_A_OFFSET 0x0100
#define XRT53_STACK_PTR_B_OFFSET 0x0104

#define XRT53_STACK_A_BASEADDR 0x0000

/* Lookup table */
#define XRT53_LUT_A_RX(SubAddr) (0x0140 + (SubAddr))   // Receive Lookup Pointer Table
#define XRT53_LUT_A_TX(SubAddr) (0x0160 + (SubAddr))   // Transmit Lookup Pointer Table
#define XRT53_LUT_A_BCST(SubAddr) (0x0180 + (SubAddr)) // Broadcast Lookup Pointer Table
#define XRT53_LUT_A_SACW(SubAddr) (0x01A0 + (SubAddr)) // SubAddress Control Word Table
#define XRT53_LUT_B_RX(SubAddr) (0x01C0 + (SubAddr))
#define XRT53_LUT_B_TX(SubAddr) (0x01E0 + (SubAddr))
#define XRT53_LUT_B_BCST(SubAddr) (0x0200 + (SubAddr))
#define XRT53_LUT_B_SACW(SubAddr) (0x0220 + (SubAddr))

/* Busy Bit Lookup Table*/
#define XRT53_LUT_BUSY(i) (0x0240 + (i))
/* Command Illegalizing Table */
#define XRT53_LUT_ILLEGAL(i) (0x0300 + (i))

// 通用地址，index从5
#define XRT53_DATABLOCK_ADDR(index) (0x0400 + 0x20 * ((index) - 5)) // 数据块对应的地址

#define XRT53_DATABLOCK_UNUSED 0x0FE0
/* SUBADDRESS CONTROL WORD bit positions*/
#define XRT53_SACW_DOUBLE_BUFFER_ENABLE 15
#define XRT53_SACW_TX_EOM_INTR 14
#define XRT53_SACW_TX_CIRC_BUF_INTR 13
#define XRT53_SACW_TX_MM2 12
#define XRT53_SACW_TX_MM1 11
#define XRT53_SACW_TX_MM0 10
#define XRT53_SACW_RX_EOM_INTR 9
#define XRT53_SACW_RX_CIRC_BUF_INTR 8
#define XRT53_SACW_RX_MM2 7
#define XRT53_SACW_RX_MM1 6
#define XRT53_SACW_RX_MM0 5
#define XRT53_SACW_BCST_EOM_INTR 4
#define XRT53_SACW_BCST_CIRC_BUF_INTR 3
#define XRT53_SACW_BCST_MM2 2
#define XRT53_SACW_BCST_MM1 1
#define XRT53_SACW_BCST_MM0 0
#define XRT53_SACW_RX_UNUSED_MASK (~0x83E0)
#define XRT53_SACW_TX_UNUSED_MASK (~0x7C00)
#define XRT53_SACW_BCST_UNUSED_MASK (~0x001F)

// 1553B子地址配置结构体 用于应用程序配置单个子地址
typedef struct XRt1553B_SA_config
{
    u32 valid_data_bytes;    // 有效字节数量 如果是0 表示该子地址不用
    u32 mm_mode;             // 存储管理模式 单消息 循环缓冲 双缓冲 见宏定义
    u32 DataBlock_number;    // 数据块编号
    u32 DataBlock_start_ptr; // 数据块起始指针 单消息自动计算
} XRt1553B_SA_config;
// mm_mode 宏定义
#define XRT53_MM_SINGLE_MSG 0
#define XRT53_MM_DOUBLE_BUFFER 0x03
#define XRT53_MM_CIRCULAR_BUFFER 0xA0
#define XRT53_MM_UNUSE 0xff

// 1553B方式指令配置
typedef struct XRt1553B_ModeCode_config
{
    u8 DynamicBusControl_IntrEnable;
    u8 Sync_IntrEnable;
    u8 TransStatWord_IntrEnable;
    u8 InitTest_IntrEnable;
    u8 TransmitterShutdown_IntrEnable;
    u8 OverrideTransmitterShutdown_IntrEnable;
    u8 InhibitTerminalFlag_IntrEnable;
    u8 OverrideInhibitTerminalFlag_IntrEnable;
    u8 ResetRemoteTerminal_IntrEnable;
    u8 TransVector_IntrEnable;
    u8 SyncwithData_IntrEnable;
    u8 TransLastCommand_IntrEnable;
    u8 TransBIT_IntrEnable;
    u8 SelTransmitterShutdown_IntrEnable;
    u8 OverrideSelTransmitterShutdown_IntrEnable;
    u8 rsv; // 凑够32bit整数倍
} XRt1553B_ModeCode_config;
// 方式代码中断使能宏
#define XRT53_MODECODE_INTR_ENABLE 1
#define XRT53_MODECODE_INTR_DISABLE 0

/* Mode Codes interrupt bit location configuration */
// RT send
#define XRT53_MODECODE_INTR_LOC_DYN_BUS_CTRL 0
#define XRT53_MODECODE_INTR_LOC_SYNC 1
#define XRT53_MODECODE_INTR_LOC_TRANS_STAT_WORD 2
#define XRT53_MODECODE_INTR_LOC_INIT_TEST 3
#define XRT53_MODECODE_INTR_LOC_TX_SHUTDOWN_ENABLE 4
#define XRT53_MODECODE_INTR_LOC_TX_SHUTDOWN_DISABLE 5
#define XRT53_MODECODE_INTR_LOC_TERMINAL_FLAG_OFF 6
#define XRT53_MODECODE_INTR_LOC_TERMINAL_FLAG_ON 7
#define XRT53_MODECODE_INTR_LOC_RESET_RT 8

#define XRT53_MODECODE_INTR_LOC_TRANS_VECTOR 0
#define XRT53_MODECODE_INTR_LOC_TRANS_LASR_CMD 2
#define XRT53_MODECODE_INTR_LOC_TRANS_BIT_WORD 3

// RT receive
#define XRT53_MODECODE_INTR_LOC_SYNC_WITH_DATA 1
#define XRT53_MODECODE_INTR_LOC_SEL_TX_SHUTDOWN_ENABLE 4
#define XRT53_MODECODE_INTR_LOC_SEL_TX_SHUTDOWN_DISABLE 5

/*
 * Callback function.  CallBackRef is a callback reference passed in by
 * the upper layer when setting the callback functions, and passed back to the
 * upper layer when the callback is invoked.
 */
typedef void (*XRt1553B_Handler)(void *CallBackRef);
typedef int XGpio;
typedef int XIntc;

typedef struct XRt1553B_Config
{
    u32 RT_address;
    struct XRt1553B_SA_config rx_SAcfg[32];   // 接收子地址配置
    struct XRt1553B_SA_config tx_SAcfg[32];   // 发送子地址配置
    struct XRt1553B_SA_config bcst_SAcfg[32]; // 广播子地址配置
    struct XRt1553B_ModeCode_config ModeCode_cfg;
    u32 Intr_mode;               // 中断或者轮询模式  0为轮询 其他为中断
    XRt1553B_Handler ISRFuncPtr; // 中断回调函数
    u16 Intr1553_Gpio_DeviceId;
    u8 Intr1553_Gpio_Intr_VectorId;
    XIntc *system_intc; // 系统中断控制器
} XRt1553B_Config;

#define XRT1553B_INTR_MODE_POLLING 0
#define XRT1553B_INTR_MODE_INTERRUPT 1


typedef struct XRt1553B
{
    UINTPTR BaseAddress; /* Device base address */
    u32 IsReady;         /* Device is initialized and ready */

    u32 RT_address;
    struct XRt1553B_SA_config rx_SAcfg[32];   // 接收子地址配置
    struct XRt1553B_SA_config tx_SAcfg[32];   // 发送子地址配置
    struct XRt1553B_SA_config bcst_SAcfg[32]; // 广播子地址配置
    struct XRt1553B_ModeCode_config ModeCode_cfg;
    u32 Intr_mode;                // 中断或者轮询模式  0为轮询 其他为中断
    XRt1553B_Handler IntrHandler; // 中断回调函数
    void *CallBackRef;            /* Callback ref for intr handler */
    XGpio Intr1553_Gpio;
    u32 CMDStack_ReadPtr; // 命令栈读指针地址 初始化为0 处理时直接用 完成后+4
} XRt1553B;

// 消息块描述符 堆栈中的四个元素
typedef struct XRt1553B_MsgBlkDesc
{
    u32 BlockStatusWord; // 块状态字
    u32 TimeTag;
    u32 DataBlkPtr_or_ModeWord; // 数据块指针或模式命令码
    u32 CMDWord;                // 命令字
    u32 RTaddr;
    u32 T_or_R;
    u32 SubAddr_or_ModeCMD;
    u32 DataLen_or_ModeCode;
    u32 BUSA_or_BUSB;
    u32 CircBuff_rollover; // 循环缓冲区回卷标志 用于循环缓冲方式
    u32 blk_err_stat;      // 错误状态 块状态字中所有错误码
} XRt1553B_MsgBlkDesc;



void XRt1553B_WriteMem(UINTPTR BaseAddress, UINTPTR RegOffset, u32 Data);
u32 XRt1553B_ReadMem(UINTPTR BaseAddress, UINTPTR RegOffset);
void XRt1553B_WriteReg(UINTPTR BaseAddress, UINTPTR RegOffset, u32 Data);
u32 XRt1553B_ReadReg(UINTPTR BaseAddress, UINTPTR RegOffset);
u32 datalenth_2_MM_mode(u32 valid_data_bytes);

#endif
